package com.xiaowu.datajpa.utils;

import com.xiaowu.datajpa.vo.QueryObj;
import org.springframework.data.domain.*;

public class PageUtil {
    public static <T>Pageable getPageable(QueryObj<T> queryObj) {
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Integer pageIndex=0;
        Integer pageSize=10;
        if(queryObj==null){
            return PageRequest.of(pageIndex,pageSize,sort);
        }else {
            pageIndex=queryObj.getPage()-1;
            pageSize=queryObj.getLimit();
            return PageRequest.of(pageIndex,pageSize,sort);
        }
    }
    public static <T>Example<T> getExample(QueryObj<T> queryObj){
        if (queryObj==null){
            return null;
        }else {
            T data = queryObj.getData();
            ExampleMatcher matcher = ExampleMatcher.matching()
                    .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                    .withIgnoreNullValues();
            Example<T> example = Example.of(data, matcher);
            return example;
        }
    }
}
